<?php
require_once INTE_ROOT . 'IGroup.php';
require_once CONF_ROOT . 'GroupConf.php';

class BaseGroup implements IGroup {

	public static function getInstance() {
		if (self :: $instance == null) {
			self :: $instance = new BaseGroup();
		}
		return self :: $instance;
	}

	protected static $instance = null;

	protected function __construct() {

	}

	/*
	 *
	 */
	public function getGroup($gid) {

		$ret = array (
			'err' => 'failed'
		);
		try {
			
			$groupData = GroupData::model()->findByPk($gid);
			if (empty ($groupData)) {
				$ret['err'] = 'not_exist';
			}
			else {
				$ret['err'] = 'ok';
				$ret['data'] = $groupData;
			}
		}
		catch (Exception $e) {
			var_dump($e);
			$ret['err'] = 'sys_err';
		}
		return $ret;

	}
	
	/*
	 *
	 */
	public function getGroupList() {

		$ret = array (
			'err' => 'failed'
		);
		try {
			
			$arr_groupData = GroupData::model()->findAll();
			if (empty ($arr_groupData)) {
				$ret['err'] = 'not_exist';
			}
			else {
				$ret['err'] = 'ok';
				$ret['data'] = $arr_groupData;
			}
		}
		catch (Exception $e) {
			var_dump($e);
			$ret['err'] = 'sys_err';
		}
		return $ret;

	}
	
		/*
	 *
	 */
	public function getGroupByName($gname) {

		$ret = array (
			'err' => 'failed'
		);
		try {
			$groupDao = new GroupData();
			$groupData = $groupDao->find('gname=:gname', array(':gname' => $gname));
			if (empty ($groupData)) {
				$ret['err'] = 'not_exist';
			}
			else {
				$ret['err'] = 'ok';
				$ret['data'] = $groupData;
			}
		}
		catch (Exception $e) {
			$ret['err'] = 'sys_err';
		}
		return $ret;

	}

	/*
	 *
	 */
	public function getPostList($gid) {

	}

	/*
	 *
	 */
	public function getUsers($gid) {

	}

	/*
	 *
	 */
	public function getLatestViewerList($gid) {

	}

	/*
	 *
	 */
	public function getTopXGroupList() {

	}

	/*
	 *
	 */
	public function getCategoryList($gid) {

	}

	/*
	 *
	 */
	public function addGroup($groupData) {

		$ret = array (
			'err' => 'failed'
		);

		try {
			$group = $this->getGroupByName($groupData->gname);
			if ('ok' == $group['err']) {
				$ret['err'] = 'err_exist';
			}
			else {
				$now = time();
				$groupData->create_time = $now;
				$groupData->last_modify_time = $now;
				$res = $groupData->save();
				if (true === $res) {
					$ret['err'] = 'ok';
				}
			}
		}
		catch (Exception $e) {
			throw $e;
			$ret['err'] = 'sys_err';
		}
		return $ret;
	}

	/*
	 *
	 */
	public function editGroup($groupData) {
		
		$ret = array (
			'err' => 'failed'
		);

		try {
			$group = $this->getGroupByGid($groupData->gid);
			if ('ok' != $group['err']) {
				$ret['err'] = 'err_not_exist';
			}
			else {
				$now = time();
				$groupData->last_modify_time = $now;
				$res = $groupData->update();
				if (true === $res) {
					$ret['err'] = 'ok';
				}
			}
		}
		catch (Exception $e) {
			$ret['err'] = 'sys_err';
		}
		return $ret;
	}

	/*
	 *
	 */
	public function addUser($uid) {

	}

	/*
	 *
	 */
	public function removeUsers($uids) {

	}

	/*
	 *
	 */
	public function setTopXGroupList($gids) {

	}

	/*
	 *
	 */
	public function setTopXGroup($gid, $rank) {

	}

	/*
	 *
	 */
	public function addCategory($gid, $catData) {

	}
	
	public function joinGroup($uid, $gid) {
		
	}
	
	public function quitGroup($uid, $gid) {
		
	}
	
	public function getJoinedGroups($uid, $pnum, $psize) {
		
	}

}